package exammanager;

import gui.CustomJScrollPane;
import gui.GUIMain;
import gui.SearchTextField;

import java.awt.BorderLayout;
import java.awt.Color;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListDataListener;

import structure.Exam;

public class GUIExamDisplay extends JPanel{
	private static final long serialVersionUID = 1L;
	
	private SearchTextField searchTF;
	private JList<Exam> display;
	private ExamModel displayModel;
	
	public GUIExamDisplay() {
		setOpaque(false);
		setLayout(new BorderLayout());
		
		searchTF = new SearchTextField();
		searchTF.getDocument().addDocumentListener(new SearchListener());
		add(searchTF, BorderLayout.NORTH);
		
		displayModel = new ExamModel();
		display = new GUIExamJList(displayModel);
		display.setCellRenderer(new GUIExamCell());
		display.setFixedCellHeight(50);
		display.setFont(getFont().deriveFont(20f));
		display.setForeground(Color.gray);
		JScrollPane pane = new CustomJScrollPane(display);
		pane.setOpaque(false);
		pane.setBorder(new EmptyBorder(2, 0, 2, 0));
		add(pane, BorderLayout.CENTER);
	}
	
	public ExamModel getDisplayModel(){
		return this.displayModel;
	}
	
	public JList<Exam> getJList(){
		return this.display;
	}
	
	public void getExamFromDB(){
		try {
			displayModel.clearExam();
			for(Exam count : GUIMain.getDb().getExams()){
				displayModel.addExam(count);
			}
			displayModel.search(searchTF.getText());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	class SearchListener implements DocumentListener{

		@Override
		public void changedUpdate(DocumentEvent e) {
			search();
		}

		@Override
		public void insertUpdate(DocumentEvent e) {
			search();
		}

		@Override
		public void removeUpdate(DocumentEvent e) {
			search();
		}
		
		public void search(){
			display.clearSelection();
			displayModel.search(searchTF.getText().trim());
		}
	}
	
	class ExamModel implements ListModel<Exam>{
		private ArrayList<Exam> collection;
		private ArrayList<Exam> displayCollection;
		
		public ExamModel() {
			collection = new ArrayList<>();
			displayCollection = new ArrayList<>();
		}
		
		public ArrayList<Exam> getCollections(){
			return this.displayCollection;
		}
		
		public void addExam(Exam param){
			collection.add(param);
		}
		
		public void clearExam(){
			displayCollection.clear();
			collection.clear();
		}
		
		@SuppressWarnings("unchecked")
		public void displayAll(){
			displayCollection = (ArrayList<Exam>) collection.clone();
		}
		
		public void search(String param){
			displayCollection.clear();
			if(param.equals("")){
				displayAll();
				display.validate();
				display.repaint();
				return;
			}
			for(Exam e : collection){
				if(e.toString().toLowerCase().contains(param.toLowerCase())){
					displayCollection.add(e);
				}
			}
			display.validate();
			display.repaint();
			
		}
		
		@Override
		public void addListDataListener(ListDataListener l) {}

		@Override
		public Exam getElementAt(int index) {
			return displayCollection.get(index);
		}

		@Override
		public int getSize() {
			return displayCollection.size();
		}

		@Override
		public void removeListDataListener(ListDataListener l) {}
	}
}